package Graphics;

import java.awt.Font;

import processing.core.PApplet;
import util.Node;
import util.Pathfinder;
import util.PublicVars;
import util.Node.NodeState;

public class Screen extends PApplet {

	int centerX = 0, centerY = 0, offsetX = 0, offsetY = 0;
	private boolean erase = false;

	public void draw() {
		background(255);
		if (mousePressed == true) {
			if (PublicVars.getActiveMode() == 0) {
				centerX = mouseX - offsetX;
				centerY = mouseY - offsetY;
			} else if (PublicVars.getActiveMode() == 1) {
				int xPos = (int) ((mouseX - centerX) / (PublicVars
						.getNodeSize() * PublicVars.getZoom()));
				int yPos = (int) ((mouseY - centerY) / (PublicVars
						.getNodeSize() * PublicVars.getZoom()));
				PublicVars.getNodeNet()[xPos][yPos].setStart(false);
				PublicVars.getNodeNet()[xPos][yPos].setTarget(false);
				if (erase) {
					PublicVars.getNodeNet()[xPos][yPos].setObstacle(false);
				} else {
					PublicVars.getNodeNet()[xPos][yPos].setObstacle(true);
				}
			}
		}

		translate(centerX, centerY);
		scale(PublicVars.getZoom());

		for (int i = 0; i < PublicVars.getNodeNet().length; i++) {
			for (int j = 0; j < PublicVars.getNodeNet()[0].length; j++) {
				if (PublicVars.getNodeNet()[i][j].getState() == NodeState.UNKNOWN) {
					fill(200, 200, 200);
				} else if (PublicVars.getNodeNet()[i][j].getState() == NodeState.CLOSEDLIST) {
					fill(200, 0, 0);
				} else {
					fill(0, 0, 200);
				}

				if (PublicVars.getNodeNet()[i][j].isObstacle()) {
					fill(0);
				} else if (PublicVars.getNodeNet()[i][j].isStart()) {
					fill(0, 255, 0);
				} else if (PublicVars.getNodeNet()[i][j].isTarget()) {
					fill(0, 255, 255);
				}

				textSize(10);
				rect(i * PublicVars.getNodeSize(),
						j * PublicVars.getNodeSize(), PublicVars.getNodeSize(),
						PublicVars.getNodeSize());
				fill(0);
				textAlign(LEFT);
				text("" + (int) PublicVars.getNodeNet()[i][j].getCosts(),
						(int) ((i + 0.2) * PublicVars.getNodeSize()),
						(int) ((j + 0.25) * PublicVars.getNodeSize()));
				text("" + (int) PublicVars.getNodeNet()[i][j].gethCosts(),
						(int) ((i + 0.2) * PublicVars.getNodeSize()),
						(int) ((j + 0.5) * PublicVars.getNodeSize()));
				// textAlign(RIGHT);
				text("" + (int) PublicVars.getNodeNet()[i][j].getgCosts(),
						(int) ((i + 0.2) * PublicVars.getNodeSize()),
						(int) ((j + 0.75) * PublicVars.getNodeSize()));
			}
		}
		int startX = 0;
		int startY = 0;
		int targetX = 0;
		int targetY = 0;
		for (int x = 0; x < PublicVars.getNodeNet().length; x++) {
			for (int y = 0; y < PublicVars.getNodeNet()[x].length; y++) {
				if (PublicVars.getNodeNet()[x][y].isStart()) {
					startX = x;
					startY = y;
				}
			}
		}
		if (PublicVars.isPathFindingFinished()) {
			for (int i = 0; i < PublicVars.getCurrentPath().size(); i++) {
				System.out.println(PublicVars.getCurrentPath().get(0));
				switch (PublicVars.getCurrentPath().get(i)) {
				case NORTH:
					targetY = startY - 1;
					break;
				case WEST:
					targetX = startX - 1;
					break;
				case EAST:
					targetX = startX + 1;
					break;
				case SOUTH:
					targetY = startY + 1;
					break;
				case SOUTHEAST:
					targetY = startY + 1;
					targetX = startX + 1;
					break;
				case SOUTHWEST:
					targetY = startY + 1;
					targetX = startX - 1;
					break;
				case NORTHEAST:
					targetY = startY - 1;
					targetX = startX + 1;
					break;
				case NORTHWEST:
					targetY = startY - 1;
					targetX = startX - 1;
					break;
				default:
					break;
				}

				int m = PublicVars.getNodeSize() / 2;
				line(startX * PublicVars.getNodeSize() + m,
						startY * PublicVars.getNodeSize() + m, targetX
								* PublicVars.getNodeSize() + m, targetY
								* PublicVars.getNodeSize() + m);
				startX = targetX;
				startY = targetY;
				System.out.println("test");
			}
		}

	}

	public void setup() {
		while (PublicVars.isInSetup()) {
			// wait
			try {
				Thread.sleep(100);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
		}
		size(600, 580);
		centerX = 0;
		centerY = 0;
		cursor(HAND);
		smooth();
	}

	public void mousePressed() {
		offsetX = mouseX - centerX;
		offsetY = mouseY - centerY;
		if ((mouseX - centerX) >= 0
				&& (mouseX - centerX) <= (PublicVars.getFieldWidth()
						* PublicVars.getNodeSize() * PublicVars.getZoom())
				&& (mouseY - centerY) >= 0
				&& (mouseY - centerY) <= (PublicVars.getFieldHeight()
						* PublicVars.getNodeSize() * PublicVars.getZoom())) {
			int xPos = (int) ((mouseX - centerX) / (PublicVars.getNodeSize() * PublicVars
					.getZoom()));
			int yPos = (int) ((mouseY - centerY) / (PublicVars.getNodeSize() * PublicVars
					.getZoom()));
			if (PublicVars.getActiveMode() == 1) {
				erase = PublicVars.getNodeNet()[xPos][yPos].isObstacle();
			} else if (PublicVars.getActiveMode() == 2) {
				for (int i = 0; i < PublicVars.getNodeNet().length; i++) {
					for (int j = 0; j < PublicVars.getNodeNet()[0].length; j++) {
						PublicVars.getNodeNet()[i][j].setStart(false);
					}
				}
				PublicVars.getNodeNet()[xPos][yPos].setObstacle(false);
				PublicVars.getNodeNet()[xPos][yPos].setTarget(false);
				PublicVars.getNodeNet()[xPos][yPos].setStart(!PublicVars
						.getNodeNet()[xPos][yPos].isStart());
			} else if (PublicVars.getActiveMode() == 3) {
				for (int i = 0; i < PublicVars.getNodeNet().length; i++) {
					for (int j = 0; j < PublicVars.getNodeNet()[0].length; j++) {
						PublicVars.getNodeNet()[i][j].setTarget(false);
					}
				}
				PublicVars.getNodeNet()[xPos][yPos].setObstacle(false);
				PublicVars.getNodeNet()[xPos][yPos].setStart(false);
				PublicVars.getNodeNet()[xPos][yPos].setTarget(!PublicVars
						.getNodeNet()[xPos][yPos].isTarget());
			}
		}
	}

	public void keyPressed() {
		if (keyCode == UP) {
			PublicVars.setZoom(PublicVars.getZoom() + 0.01f);
		}
		if (keyCode == DOWN) {
			PublicVars.setZoom(PublicVars.getZoom() - 0.01f);
		}
		if (keyCode == ' ') {
			PublicVars.setActiveMode((1 + PublicVars.getActiveMode()) % 4);
		}
		Pathfinder.getMainframe().getPanelSettings().refresh();
	}

}
